
import React from 'react';
import { inject, observer } from 'mobx-react';
import { WeaInput , WeaBrowser,WeaFormItem , WeaSelect , WeaTop ,WeaInputSearch,WeaHelpfulTip,WeaLocaleProvider} from 'ecCom';
import {WeaSwitch} from 'comsMobx';
import { Table , Button ,Icon ,Modal ,message,Tooltip} from 'antd';
import * as mobx from 'mobx';

import * as API_CUSTOMREPORTADD from '../../../apis/customReportAdd';
import TipsModal from './TipsModal';
const getLabel = WeaLocaleProvider.getLabel;

@inject('wfCusReportAddStore')

@observer
export default class BaseInfo extends React.Component{
    constructor(props){
        super(props);
        this.state={
            tipShowInfo:'',
            tipsModalShow:false
        }
    }
    componentDidMount() {
        const {wfCusReportAddStore} = this.props;
        const { initDatas ,baseBrowserViewAttr,reportid} = wfCusReportAddStore;
        initDatas();//
    }
    workflowPathChange(id,names,datas){
        
        const { wfCusReportAddStore } =this.props;
        const {reportBaseinfo,workflowPathOnchange,wfReplaceDatas,setBaseInfo,formBrowserId} = wfCusReportAddStore;

        let formid = "";
        let selectedPlaceDatas=[];
        if(datas.length>0){
            formid = datas[0].formid;
            let notSameFlag = false;
            datas&&datas.map(v=>{
                if(v.formid == formid){
                    selectedPlaceDatas.push(v);
                }else{
                    notSameFlag = true;//有不同表单的路径
                }
            });
            if(notSameFlag){
                this.setState({
                    tipShowInfo:getLabel(386270,' 不可选择使用不同表单的路径！ '),
                    tipsModalShow:true
                })
                this.refs.workflow_ref.set(reportBaseinfo.reportRoute);
            }else{
                let wfId = datas[0].id;
                API_CUSTOMREPORTADD.getFormInfo({workflowid:wfId}).then((data)=>{
                    let palceDatas = data.replaceDatas || [];
                    if(palceDatas.length>0){
                        workflowPathOnchange(palceDatas,{reportRoute : selectedPlaceDatas},selectedPlaceDatas);
                    }
                })
            }
        }else{
            setBaseInfo({reportRoute:[],workflowToFormid:'',workflowToIsbill:''});//清空路径时设置store数据为空
        }
    }
    render(){
        const { wfCusReportAddStore } =this.props;
        const {reportBaseinfo,setBaseInfo,formBrowser,wfBrowser,baseBrowserViewAttr,formReplaceDatas,baseInfoFormChange,wfReplaceDatas,
            formBrowserId,formBrowserIsbill,wfBrowserShow,form} = wfCusReportAddStore;
        
        const { tipShowInfo , tipsModalShow} = this.state;
        const formParams = form.getFormParams();
        let conditions = this.getCondition();
        return(
            <div className="custom-report-content">
            <div className="baseinfo">
                <TipsModal ecId={`${this && this.props && this.props.ecId || ''}_TipsModal@nxvygw`}
                    visible={tipsModalShow}
                    showInfo={tipShowInfo}
                    cancelHandle={()=>{
                        this.setState({
                            tipShowInfo:'',
                            tipsModalShow:false
                        })
                    }}
                />
                {conditions}
            </div>
        </div>
        )
    }
    getCondition(){
        let group = [];
        const {wfCusReportAddStore} = this.props;
        const { formBrowserId,formBrowserIsbill,wfReplaceDatas,setBaseInfo,baseInfoFormChange,reportBaseinfo,formReplaceDatas,baseBrowserViewAttr,firstChoose,reportid} = wfCusReportAddStore;
        const { condition,form,appendFormFields} = wfCusReportAddStore;
        
        const formParams = form.getFormParams();
        
        let filterFormid = '';
        let filterIsbill = '';
        if(formBrowserId&&formBrowserId!=""){
            filterFormid = formBrowserId;
            filterIsbill = formBrowserIsbill;
        }else{
            filterFormid = reportBaseinfo.workflowToFormid;
            filterIsbill = reportBaseinfo.workflowToIsbill;
        }
        form.isFormInit&&condition.map(v =>{
            let items = [];
            v.items.map(item => {
                if(item.domkey[0]=='path'){
                    items.push(
                        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@ulvhdd@${item.domkey[0]}`}
                            label={`${item.label}`}
                            labelCol={{span: `${item.labelcol}`}}
                            wrapperCol={{span: `${item.fieldcol}`}}>
                            <div className="worklfow_path">
                            <WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@1ozkiw@${item.domkey[0]}`}
                                {...item.browserConditionParam}
                                ref = {'workflow_ref'}
                                isSingle = {false}
                                dataParams={(firstChoose==2||baseBrowserViewAttr==1)?{formid:filterFormid,isbill:filterIsbill}:{}}
                                completeParams={(firstChoose==2||baseBrowserViewAttr==1)?{formid:filterFormid,isbill:filterIsbill}:{}}
                                replaceDatas={reportBaseinfo.reportRoute.slice()}
                                onChange={(id,names,datas)=>{
                                    this.workflowPathChange(id,names,datas);
                                }}
                            />
                            <WeaHelpfulTip ecId={`${this && this.props && this.props.ecId || ''}_WeaHelpfulTip@6nsyqj@${item.domkey[0]}`} title={getLabel(386258,' 不选择表示使用对应表单的所有流程路径 ')} width={210}/>
                            </div>
                        </WeaFormItem>
                    );
                }else if(item.domkey[0]=='form'){
                    items.push(
                        <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@6xjebi@${item.domkey[0]}`}
                            label={`${item.label}`}
                            labelCol={{span: `${item.labelcol}`}}
                            wrapperCol={{span: `${item.fieldcol}`}}
                            error={form.getError(item)}
                            tipPosition={'bottom'}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@fmblne@${item.domkey[0]}`} fieldConfig={item} form={form} onChange={(v,data)=>{
                                setBaseInfo({reportList : data.valueObj,});
                                baseInfoFormChange(data.value,data.valueSpan,data.valueObj);
                                // form.updateFields({form:{value:data.value,valueSpan:data.valueSpan,valueObj:data.valueObj}},false);
                            }}/>
                        </WeaFormItem>
                    );
                }else{
                    item.className = 'cus_report_name';
                    item.inputType='multilang' ;
                    item.defaultValue = reportBaseinfo.reportName;
                    items.push(
                       <WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@bgjnsj@${item.domkey[0]}`}
                            label={`${item.label}`}
                            labelCol={{span: `${item.labelcol}`}}
                            wrapperCol={{span: `${item.fieldcol}`}}
                            error={form.getError(item)}
                            tipPosition={'bottom'}>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@uqr2ck@${item.domkey[0]}`} fieldConfig={item} form={form} onChange={(v)=>{
                                if(item.domkey[0]=='reportname') {
                                    let endObj = this.checkNameLen(100,v.reportname.value);
                                    if(endObj.isPassed){
                                        setBaseInfo({reportName : v.reportname.value})
                                    }else{
                                        // message.destroy();
                                        // message.warning('报表名称最多不能超过100个字符！');
                                        setTimeout(()=>{
                                            appendFormFields({reportname:endObj.endStr});
                                            setBaseInfo({reportName : endObj.endStr})
                                        },10)
                                    }
                                }
                                setBaseInfo({});
                                }}/>
                        </WeaFormItem>
                    );
                }
            });
            group = items;
        });
        return <div>{group}</div>
    }
    checkNameLen(endLen,typeName){
        let codeLen = 0,endStr=typeName,isPassed=true;
        for(let i=0,len = typeName.length;i<len;i++){
            if ((typeName.charCodeAt(i)<=255)){
                codeLen++;
                if(codeLen>endLen){
                    isPassed = false;
                    endStr = typeName.substring(0,i);
                    break;
                }
            }else{
                codeLen+=3;
                if(codeLen>endLen){
                    isPassed = false;
                    endStr = typeName.substring(0,i);
                    break;
                }
            }
        }
        return{endStr,isPassed}
    }
}
